//package keymove;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class GameStart extends JFrame implements Runnable
{
	private static final long serialVersionUID = 5642619743815256222L;

	private Thread game;
	private boolean isRunning = false;
	private int width, height;
	private BufferStrategy myStrategy;
	
	/**
	 *  Player, only a square currently
	 */
	private Player player;
	// initializae a 2nd player
	private Player player2;

	public GameStart()
	{
		setUndecorated(true);
		setResizable(false);
		setIgnoreRepaint(true);

		/**
		 * Init FSEM Mode & Create Buffer for Page Flipping
		 */
		GraphicsDevice myDevice;
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		myDevice = ge.getDefaultScreenDevice();
		if (!myDevice.isFullScreenSupported())
		{
			System.out.println("Full-screen exclusive mode not supported");
			System.exit(0);
		}
		try
		{
			myDevice.setFullScreenWindow(this);
			EventQueue.invokeAndWait(new Runnable() {
				public void run()
				{
					createBufferStrategy(2);
				}
			});
		}
		catch (Exception e)
		{
			System.out.println("Error while creating buffer strategy");
			System.exit(0);
		}

		setVisible(true);
		width = getBounds().width;
		height = getBounds().height;
		player = new Player(width, height);

		/*
		 * Add key event listener to monitor arrow key press
		 * and release event
		 */
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_UP)
				{
					player.setUp(true);
				}
				if (e.getKeyCode() == KeyEvent.VK_DOWN)
				{
					player.setDown(true);
				}
				if (e.getKeyCode() == KeyEvent.VK_LEFT)
				{
					player.setLeft(true);
				}
				if (e.getKeyCode() == KeyEvent.VK_RIGHT)
				{
					player.setRight(true);
				}
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
				{
					isRunning = false;
					System.exit(0);
				}
			}
			public void keyReleased(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_UP)
				{
					player.setUp(false);
				}
				if (e.getKeyCode() == KeyEvent.VK_DOWN)
				{
					player.setDown(false);
				}
				if (e.getKeyCode() == KeyEvent.VK_LEFT)
				{
					player.setLeft(false);
				}
				if (e.getKeyCode() == KeyEvent.VK_RIGHT)
				{
					player.setRight(false);
				}
			}
		});

		startgame();
	}

	/**
	 * Launch Game Thread
	 */
	public void startgame()
	{
		if (game == null || !isRunning)
		{
			game = new Thread(this);
			game.start();
		}
	}

	public void run()
	{
		isRunning = true;
		// TODO Auto-generated method stub
		while (isRunning)
		{
			gameUpdate();
			myStrategy = getBufferStrategy();
			Graphics graphics = myStrategy.getDrawGraphics();
			try
			{
				graphicUpdate(graphics);
			}
			finally
			{
				graphics.dispose();
			}
			myStrategy.show();
			
			try
			{
				Thread.sleep(30);
			}
			catch (InterruptedException ie)
			{
				
			}
		}

	}

	/**
	 * Game Sprite and background updates
	 */
	private void gameUpdate()
	{
		// all players move action here
		if (isRunning)
		{
			player.move();
			player2.randomv();
		}
	}

	/**
	 * Repaint every element in order
	 * 
	 * @param g Current Drawing Graphic
	 */
	private void graphicUpdate(Graphics g)
	{
		g.setColor(Color.BLACK);
	    g.fillRect (0, 0, width, height);
	    player.draw(g);
	}
}
